/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.manto;

import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.manto.AgendaReunionForm;
import com.uca.sisbec.helpers.manto.AgendaReunionHelper;
import com.uca.sisbec.helpers.manto.ReunionHelper;
import com.uca.sisbec.mapping.Reunion;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

/**
 *
 * @author Alex
 */
public class AgendaReunionAction extends HibernateAction {
    public ActionForward init(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ReunionHelper rh = new ReunionHelper(getSession(request));
        Reunion r = rh.BuscarReunionDB(request.getParameter("codR"));
        if(r.getAgendaXReunions() != null){
            request.setAttribute("AgendaList", r.getAgendaXReunions());
        }
        request.setAttribute("Reunion", r);
        return mapping.findForward("success");
    }
    
    public ActionForward GuardarPuntoAgenda(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        AgendaReunionForm arf = (AgendaReunionForm) form;
        ActionMessages msg = new ActionMessages();
        ActionErrors errors = new ActionErrors();
        ReunionHelper rh = new ReunionHelper(getSession(request));
        AgendaReunionHelper arh = new AgendaReunionHelper(getSession(request));
        if(arh.ExistePuntoAgendaSimilarDB(arf)){
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("puntoagenda.existe"));
            request.setAttribute("errors", errors);
        }else {
            arh.InsertarPuntoAgendaDB(arf);
            getSession(request).flush();
            msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("puntoagenda.nuevo"));
            request.setAttribute("msg", msg);
        }
        Reunion r = rh.BuscarReunionDB(String.valueOf(arf.getIdReunion()));
        request.setAttribute("Reunion", r);
        request.setAttribute("AgendaList", r.getAgendaXReunions());
        return mapping.findForward("success");
    }
      
    public ActionForward EliminarPuntoAgenda(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        AgendaReunionForm arf = (AgendaReunionForm) form;
        ActionMessages msg = new ActionMessages();
        ActionErrors error = new ActionErrors();
        AgendaReunionHelper arh = new AgendaReunionHelper(getSession(request));
        arh.EliminarPuntoAgendaDB(Integer.parseInt(request.getParameter("idR")), Integer.parseInt(request.getParameter("idPtoAg")));
        getSession(request).flush();
        ReunionHelper rh = new ReunionHelper(getSession(request));
        Reunion r = rh.BuscarReunionDB(request.getParameter("idR"));
        request.setAttribute("Reunion", r);
        request.setAttribute("AgendaList", r.getAgendaXReunions());
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("puntoagenda.eliminado"));
        request.setAttribute("msg", msg);
        return mapping.findForward("success");
    }
    
}
